<K, V>
map <K, V>
clone
pub fn clone(self): <T, T>
Shallow copy a map.
clone
pub fn clone(self): <T, T>
Shallow copy a map.
Shallow copy a map.
let items = {
"name": "Peter",
};
let items2 = items.clone();
items2["name"] = "Jason";
assert_eq items["name"], "Peter";
assert_eq items2["name"], "Jason";
values
pub fn values(self): [T]
Return all values in the map.
values
pub fn values(self): [T]
Return all values in the map.
Return all values in the map.
let items = {
"name": "Peter",
"age": "20",
"country": "US",
};
assert_eq items.values(), ["Peter", "20", "US"];
keys
pub fn keys(self): [T]
Return all keys in the map.
keys
pub fn keys(self): [T]
Return all keys in the map.
Return all keys in the map.
let items = {
"name": "Peter",
"age": "20",
"country": "US",
};
assert_eq items.keys(), ["name", "age", "country"];
clear
pub fn clear(self)
Remove all elements from the map.
clear
pub fn clear(self)
Remove all elements from the map.
Remove all elements from the map.
let items = {
"name": "Peter",
"age": "20",
};
items.clear();
assert_eq items.len(), 0;
remove
pub fn remove(self, key: T)
Remove a key from the map.
If the key does not exist, this function does nothing.
remove
pub fn remove(self, key: T)
Remove a key from the map. If the key does not exist, this function does nothing.
Remove a key from the map. If the key does not exist, this function does nothing.
let items = {
"name": "Peter",
};
items.remove("name");
assert_eq items.len(), 0;
items.remove("name"); // does nothing
set
pub fn set(self, key: T, value: T)
Set a key-value pair into the map.
set
pub fn set(self, key: T, value: T)
Set a key-value pair into the map.
Set a key-value pair into the map.
If the key already exists, the value will be replaced.
let items: <string, string> = {:};
items.set("name", "Peter");
items.set("age", "20");
items.len(); // 2
items.set("age", "21");
items.len(); // 2
Or you can just use the []
operator:
let items: <string, string> = {:};
items["name"] = "Peter";
items["age"] = "20";
get
pub fn get(self, key: T): T?
Get a value from the map.
get
pub fn get(self, key: T): T?
Get a value from the map.
Get a value from the map.
If you use get
will return a tuple (value, true)
if the key exists, otherwise (nil, false)
.
let items: <string, string> = {:};
items.set("name", "Peter");
items.set("age", "20");
items.get("name"); // "Peter", true
Or use the []
operator, this will just return the value, if the key does not exist, it will return nil
.
items["name"]; // "Peter"
items["not-exist"]; // nil
contains_key
pub fn contains_key(self, key: T): bool
contains_key
pub fn contains_key(self, key: T): bool